<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="盒子分类" prop="fid">
        <el-select v-model="queryParams.fid" placeholder="请选择">
          <el-option
            v-for="item in listFl"
            :key="item.title"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="盒子名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入盒子名称"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="盒子状态" prop="status">
        <el-select
          v-model="queryParams.status"
          clearable
          placeholder="请选择盒子状态"
          
        >
          <el-option
            v-for="item in [
              { label: '删除', value: '4' },
              {
                value: '3',
                label: '售罄',
              },
              {
                value: '2',
                label: '下架',
              },
              {
                value: '1',
                label: '上架',
              },
            ]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="排序值" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序值"
          clearable
          
        />
      </el-form-item> -->
      <el-form-item label="盒子价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入盒子价格"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="是否现货" prop="isYu">
        <el-input
          v-model="queryParams.isYu"
          placeholder="请输入是否现货 1现货  2 预售"
          clearable
          
        />
      </el-form-item>
      <!-- <el-form-item label="到货时间" prop="arriveTime">
        <el-input
          v-model="queryParams.arriveTime"
          placeholder="请输入到货时间"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="套数" prop="stock">
        <el-input
          v-model="queryParams.stock"
          placeholder="请输入套数"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="限购次数 0 不限制" prop="xgBuy">
        <el-input
          v-model="queryParams.xgBuy"
          placeholder="请输入限购次数 0 不限制"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="抽几次开启全收 0不限制" prop="xgAll">
        <el-input
          v-model="queryParams.xgAll"
          placeholder="请输入抽几次开启全收 0不限制"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="全收0关闭 1开启" prop="kgAll">
        <el-input
          v-model="queryParams.kgAll"
          placeholder="请输入全收0关闭 1开启"
          clearable
          
        />
      </el-form-item> -->
      <el-form-item label="添加时间" prop="addtime">
        <el-input
          v-model="queryParams.addtime"
          placeholder="请输入添加时间"
          clearable
          
        />
      </el-form-item>
      <!-- <el-form-item label="${comment}" prop="creator">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入${comment}"
          clearable
          
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="createDate">
        <el-date-picker clearable
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择${comment}">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:goods:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:goods:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:goods:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:goods:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="goodsList"
      @selection-change="handleSelectionChange"
      @sort-change="dataListSortChangeHandle"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="盒子ID" align="center" prop="id" />
      <!-- <el-table-column label="分类id" align="center" prop="fid">
        <template slot-scope="scope">
          {{ class_box[scope.row.fid] }}
        </template>
      </el-table-column> -->
      <el-table-column label="盒子名称" align="center" prop="title" />
      <el-table-column label="盒子封面图" align="center" prop="imgurl">
        <template slot-scope="scope">
          <el-image :src="scope.row.imgurl" style="width: 50px; height: 50px" />
        </template>
      </el-table-column>
      <el-table-column label="排序值" align="center" prop="sort" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status == 1">上架</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 2">下架</el-tag>
          <el-tag type="warning" v-if="scope.row.status == 3">售罄</el-tag>
          <el-tag type="info" v-if="scope.row.status == 4">删除</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="盒子价格"
        sortable="custom"
        align="center"
        prop="price"
      />
      <el-table-column label="是否现货" align="center" prop="isYu">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isYu == 1">现货</el-tag>
          <el-tag v-if="scope.row.isYu == 0">预售</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="到货时间" align="center" prop="arriveTime" /> -->
      <!-- <el-table-column
        label="类型 1 一番赏 2 双随机"
        align="center"
        prop="type"
      /> -->
      <el-table-column
        sortable="custom"
        label="套数"
        align="center"
        prop="stock"
      />
      <el-table-column label="限购次数" align="center" prop="xgBuy">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.xgBuy == 0">不限制</el-tag>
          <span v-else> {{ scope.row.xgBuy }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="抽几次开启全收"
        align="center"
        prop="xgAll"
        sortable="custom"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.xgAll == 0">不限制</el-tag>
          <el-tag v-else>{{ scope.row.xgAll }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否全收" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.kgAll == 1">是</el-tag>
          <el-tag v-else>否</el-tag>

          <!-- <el-switch
            v-model.number="scope.row.kgAll"
            active-color="#13ce66"
            inactive-color="#F4F4F5"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch> -->
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="addtime">
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.addtime, "{y}-{m}-{d} {hh}:{mm}:{ss}")
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="${comment}" align="center" prop="creator" />
      <el-table-column label="${comment}" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:list:edit']"
            >一键导出</el-button
          >
          <!-- <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            @click="handleEditTable(scope.row)"
            >添加盒子</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:goods:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改盒子信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类id" prop="fid">
          <el-select v-model="form.fid" placeholder="请选择">
            <el-option
              v-for="item in listFl"
              :key="item.title"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="盒子名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入盒子名称" />
        </el-form-item>

        <el-form-item label="是否上架">
          <el-radio-group v-model.number="form.status">
            <el-radio :label="1">上架</el-radio>
            <el-radio :label="2">下架</el-radio>
            <el-radio :label="3">售罄</el-radio>
            <el-radio :label="4">删除</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="盒子封面图" prop="imgurl">
          <!-- <el-upload
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-change="handleChange"
            :file-list="fileList"
          >
            <el-image
              style="width: 100px; height: 100px"
              :src="form.imgurl"
              :preview-src-list="srcList"
            >
            </el-image>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload> -->
          <image-upload v-model="form.imgurl" />
        </el-form-item>
        <el-form-item label="排序值" prop="sort">
          <el-input-number
            v-model="form.orderNum"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item label="盒子价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入盒子价格" />
        </el-form-item>
        <el-form-item label="是否现货" prop="isYu">
          <el-switch
            v-model.number="form.isYu"
            active-color="#13ce66"
            inactive-color="#F4F4F5"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
          <!-- <el-input
            v-model="form.isYu"
            placeholder="请输入是否现货 1现货  2 预售"
          /> -->
        </el-form-item>
        <!-- <el-form-item label="到货时间" prop="arriveTime">
          <el-date-picker
            v-model="form.arriveTime"
            type="date"
            placeholder="选择到货时间"
          >
          </el-date-picker>
         
        </el-form-item> -->
        <!-- 
        <el-form-item label="盒子描述" prop="desc">
          <el-input v-model="form.desc" placeholder="请输入盒子描述" />
        </el-form-item> -->
        <!-- <el-form-item label="显示按钮" prop="desc">
          <template>
            <el-tag
              :key="tag"
              v-for="tag in form.dynamicTags"
              closable
              :disable-transitions="false"
              @close="handleClose(tag)"
            >
              {{ tag }}
            </el-tag>
            <el-input
              class="input-new-tag"
              v-if="inputVisible"
              v-model="inputValue"
              ref="saveTagInput"
              size="small"
              
              @blur="handleInputConfirm"
            >
            </el-input>
            <el-button
              v-else
              class="button-new-tag"
              size="small"
              @click="showInput"
              >+ 抽数按钮</el-button
            >
            <el-input
              v-if="form.ifxg_buy"
              v-model="form.ifxg_btn_num"
              placeholder="抽几次全收"
            ></el-input>
          </template>
        </el-form-item> -->
        <el-form-item label="套数" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入套数" />
        </el-form-item>
        <el-form-item label="限购次数" prop="xgBuy">
          <!-- <el-input
            v-model="form.xgBuy"
            placeholder="请输入限购次数 0 不限制"
          /> -->
          <el-switch
            v-model.number="form.xgBuy"
            active-color="#13ce66"
            inactive-color="#F4F4F5"
            :active-value="form.xgBuy != 0 ? form.xgBuy : 1"
            :inactive-value="0"
            :active-text="form.xgBuy != 0 ? '请在下面输入限购次数   ' : ''"
          >
          </el-switch>

          <el-input-number
            v-if="form.xgBuy != 0"
            v-model="form.xgBuy"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item label="全收阈值" prop="xgAll">
          <el-switch
            v-model.number="form.xgAll"
            active-color="#13ce66"
            inactive-color="#F4F4F5"
            :active-value="form.xgAll != 0 ? form.xgAll : 1"
            :inactive-value="0"
            :active-text="form.xgAll != 0 ? '请在下面输入抽几次开启全收' : ''"
          >
          </el-switch>
          <el-input-number
            v-if="form.xgAll != 0"
            v-model="form.xgAll"
            controls-position="right"
            :min="0"
          />
          <!-- <el-input
           
            v-model="form.xgAll"
            placeholder="请输入抽几次开启全收 0不限制"
          /> -->
        </el-form-item>
        <el-form-item label="是否全收" prop="kgAll">
          <el-switch
            v-model.number="form.kgAll"
            active-color="#13ce66"
            inactive-color="#F4F4F5"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="支付方式" prop="isJf">
          <el-select v-model="form.fid" placeholder="请选择挂载模块">
            <el-option
              v-for="dict in dict.type.pay_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="产生积分" prop="isJf">
          <el-switch
            v-model.number="form.isJf"
            active-color="#13ce66"
            inactive-color="#F4F4F5"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item>

        <el-form-item label="产生流水" prop="isJf">
          <el-switch
            v-model.number="form.isJf"
            active-color="#13ce66"
            inactive-color="#F4F4F5"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 快捷导入 -->
    <el-dialog
      title="快捷导入"
      :visible.sync="dialogVisible"
      width="30%"
      center
      :close-on-click-modal="false"
    >
      <importForm />
    </el-dialog>

   
  </div>
</template>

<script>
import {
  listGoods,
  getGoods,
  delGoods,
  addGoods,
  updateGoods,
} from "@/api/yfs/box.js";
import { listFl } from "@/api/yfs/box_class.js";
import importForm from "@/components/importForm";
export default {
  dicts: ["pay_type"],
  name: "Goods",
  data() {
    return {
      
      //是否打开快捷导入
      dialogVisible: false,

      inputVisible: false,
      inputValue: "",

      //盒子分类列表
      listFl: [],
      class_box: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 盒子信息表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数

      queryParams: {
        isAsc: "desc", // 排序，asc／desc
        orderByColumn: "updateDate", // 排序，字段
        pageNum: 1,
        pageSize: 10,
        fid: null,
        title: null,
        imgurl: null,
        sort: null,
        status: null,
        price: null,
        isYu: null,
        arriveTime: null,
        type: null,
        stock: null,
        xgBuy: null,
        xgAll: null,
        kgAll: null,
        addtime: null,
        creator: null,
        createDate: null,

        ifxg_btn_num: null,
        ifxg_btn_all: null,
        dynamicTags: ["hh"],
      },
      // 表单参数
      form: {
        dynamicTags: [],
      },
      // 表单校验
      rules: {
        fid: [{ required: true, message: "分类id不能为空", trigger: "blur" }],
        title: [
          { required: true, message: "盒子名称不能为空", trigger: "blur" },
        ],
        imgurl: [
          { required: true, message: "盒子封面图不能为空", trigger: "blur" },
        ],
        status: [
          {
            required: true,
            message: "1上架 2 下架  3 售罄 4删除 不能为空",
            trigger: "blur",
          },
        ],
        xgBuy: [
          {
            required: true,
            message: "限购次数 0 不限制不能为空",
            trigger: "blur",
          },
        ],
        xgAll: [
          {
            required: true,
            message: "抽几次开启全收 0不限制不能为空",
            trigger: "blur",
          },
        ],
        kgAll: [
          {
            required: true,
            message: "全收0关闭 1开启不能为空",
            trigger: "blur",
          },
        ],
        addtime: [
          { required: true, message: "添加时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  components: {
    importForm,
  },
  created() {
    this.getList();
    this.getFlList();
  },
  methods: {
    handleClose(tag) {
      console.log(tag);
      if (tag == "全收") {
        this.form.ifxg_buy = 0;
      }
      this.form.dynamicTags.splice(this.form.dynamicTags.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.form.dynamicTags.push(inputValue);
      }
      if (this.inputValue == "全收") {
        this.form.ifxg_buy = 1;
      }
      console.log(this.inputValue);
      this.inputVisible = false;
      this.inputValue = "";
    },

    //获取分类列表
    getFlList() {
      listFl().then((res) => {
        console.log(res);
        this.listFl = res.rows;
        console.log(this.listFl);

        this.listFl.forEach((item) => {
          this.class_box[item.id] = item.title;
        });

        this.listFl = this.listFl.map((item) => {
          return {
            id: item.id,
            title: item.title,
          };
        });
        console.log(this.class_box);
      });
    },

    /** 查询盒子信息列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then((response) => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fid: null,
        title: null,
        imgurl: null,
        sort: null,
        status: 0,
        price: null,
        isYu: null,
        arriveTime: null,
        type: null,
        stock: null,
        xgBuy: null,
        xgAll: null,
        kgAll: null,
        addtime: null,
        creator: null,
        createDate: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.orderByColumn = "";
      this.queryParams.isAsc = "";
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加盒子信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getGoods(id).then((response) => {
        this.form = response.data;
        console.log(this.form);
        if (this.form.payType) {
          this.form.payType = this.form.payType.split(",");
        }
        //发送数据到父组件
        this.$emit("func", this.form);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除盒子信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delGoods(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/goods/export",
        {
          ...this.queryParams,
        },
        `goods_${new Date().getTime()}.xlsx`
      );
    },
    /** 添加盒子 */
    handleEditTable(row) {
      const tableId = row.id;
      const tableName = row.title;
      //   const params = { pageNum: this.queryParams.pageNum };
      console.log(row);
      this.$tab.openPage(
        "添加[" + tableName + "]盒子盒子",
        "/yfs/add_good/index/" + tableId
      );
    },
    //排序
    dataListSortChangeHandle(data) {
      if (!data.order || !data.prop) {
        this.queryParams.orderByColumn = "";
        this.queryParams.isAsc = "";
        return false;
      }
      this.queryParams.isAsc = data.order.replace(/ending$/, "");
      this.queryParams.orderByColumn = data.prop
        .replace(/([A-Z])/g, "_$1")
        .toLowerCase();
      this.getList();
    },
  },
};
</script>
